//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_UART_H__
#define __ELASTOS_UART_H__

#define COMPORT_AVAILABLE(comPort) \
        (ComPort1 == (comPort) || ComPort2 == (comPort))

//
// CPLD structure
//
typedef struct {
    uint8_t keyState;
    uint8_t noUse11;
    uint8_t noUse12;
    uint8_t noUse13;

    uint8_t intState;
    uint8_t noUse21;
    uint8_t noUse22;
    uint8_t noUse23;

    uint8_t intMask;
    uint8_t noUse31;
    uint8_t noUse32;
    uint8_t noUse33;

    uint8_t pwrIO;
    uint8_t noUse41;
    uint8_t noUse42;
    uint8_t noUse43;

    uint8_t extIO;
} CPLDREGS;

//
// CPLD power control io
//
#define USB_PWR      __8BIT(7)
#define ETH_PWR      __8BIT(6)
#define RS232_PWR    __8BIT(5)
#define IRDA_PWR     __8BIT(4)
#define LCD_PWR      __8BIT(3)
#define LIGHT_PWR    __8BIT(2)
#define AUDIO_PWR    __8BIT(1)
#define CF_PWR       __8BIT(0)

//#define MyCPLD   ((CPLDREGS *)(0x20000000))
//
//INLINE void MyCPLD_SetPwrIO(uint_t pwrNum, uint_t state)
//{
//    if (state) {
//        MyCPLD->pwrIO |= pwrNum;
//    }
//    else {
//        MyCPLD->pwrIO &= ~pwrNum;
//    }
//}

// UART IO base address
#define _UART1_IOBASE       ((ioport_t)0x80000600)
#define _UART2_IOBASE       ((ioport_t)0x80000700)
#define _UART3_IOBASE       ((ioport_t)0x80000800)

#define COMPORT_IOBASE(comPort) \
        ((ComPort1 == (comPort))? _UART1_IOBASE : \
            ((ComPort2 == (comPort))? _UART2_IOBASE : _UART3_IOBASE))

// UART registers' address
#define _UART_DATA(base)    ((ioport_t)((base) + 0x00))
#define _UART_FCON(base)    ((ioport_t)((base) + 0x04))
#define _UART_BRCON(base)   ((ioport_t)((base) + 0x08))
#define _UART_CON(base)     ((ioport_t)((base) + 0x0c))
#define _UART_STATUS(base)  ((ioport_t)((base) + 0x10))
#define _UART_RAWISR(base)  ((ioport_t)((base) + 0x14))
#define _UART_INTEN(base)   ((ioport_t)((base) + 0x18))
#define _UART_ISR(base)     ((ioport_t)((base) + 0x1c))

// _UART_DATA register
#define _FE                 0x00000100
#define _PE                 0x00000200
#define _OE                 0x00000400
#define _BE                 0x00000800
#define _ERRMASK            0x00000f00

// _UART_BRCON register
#define _BAUDDIV_460800     0x00000000
#define _BAUDDIV_230400     0x00000001
#define _BAUDDIV_153600     0x00000002
#define _BAUDDIV_115200     0x00000003
#define _BAUDDIV_76800      0x00000005
#define _BAUDDIV_57600      0x00000008
#define _BAUDDIV_38400      0x0000000b
#define _BAUDDIV_28800      0x0000000f
#define _BAUDDIV_19200      0x00000017
#define _BAUDDIV_9600       0x0000004c
#define _BAUDDIV_4800       0x0000005f
#define _BAUDDIV_2400       0x000000bf

// _UART_FCON register
#define _BRK            __32BIT(0)
#define _PEN            __32BIT(1)
#define _EPS            __32BIT(2)
#define _STP2           __32BIT(3)
#define _FEN            __32BIT(4)
#define _WLEN_5         0x00000000
#define _WLEN_6         0x00000020
#define _WLEN_7         0x00000040
#define _WLEN_8         0x00000060

// _UART_CON register
#define _UARTEN         __32BIT(0)
#define _SIRD           __32BIT(1)
#define _SIRLP          __32BIT(2)
#define _RXP            __32BIT(3)
#define _TXP            __32BIT(4)
#define _MXP            __32BIT(5)
#define _LBE            __32BIT(6)
#define _SIRBD          __32BIT(7)

// _UART_STATUS register
#define _CTS            __32BIT(0)
#define _DSR            __32BIT(1)
#define _DCD            __32BIT(2)
#define _BUSY           __32BIT(3)
#define _RXFE           __32BIT(4)
#define _TXFF           __32BIT(5)
#define _RXFF           __32BIT(6)
#define _TXFE           __32BIT(7)

// _UART_INTEN register and _UART_ISR register
#define _RI             __32BIT(0)
#define _TI             __32BIT(1)
#define _MI             __32BIT(2)
#define _RTI            __32BIT(3)

#endif // __ELASTOS_UART_H__
